package com.pat.jrgwt.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;

public class ProfitLossesPipsPor3 extends BaseMonthTableView
{

    private static class SingletonHolder
    {
        private static ProfitLossesPipsPor3 instance = new ProfitLossesPipsPor3();
    }
    
    public static ProfitLossesPipsPor3 getInstance()
    {
        return SingletonHolder.instance;
    }
    
    private ProfitLossesPipsPor3()
    {
        setLabel("Profit/losses (Pips)");
    }
    
    
    @Override
    public void onYearChanged(int year)
    {
        Window.alert("Year is changed to: " + year);
    }

    @Override
    public void recalculate()
    {
//        m_arCellValues = new double[m_arRowLabels.length][m_arHeaders.length];
        m_arCellValues = new double[][]{
                { 0, 227, 57, 0, 24.5, 0, 0, 0, 119, 74.6, 0}
                , { 0, 151.5, 58, 0, -9, -6.5, 0, 0, -15.3, 76.8, 0}
                , { 0, 8, 86, 0, -31.5, -22, 0, 0, 0, 0, 0}
                , { 0, -314.5, -74, 0, -30.75, -63.75, 0, 0, 532, 579.5, 0}
                , { 0, -225, -116, 0, -31.25, -18, 0, 0, 0, 0, 0}
                , { 0, 323.95, -80, 0, 151.15, 42, 0, 190, 0, 0, 0}
                , { 0, -345.12, -25, 0, -12.6, -65.75, 273.57, 194, 0, 0, 0}
                , { 577, 1205.85, 258, 42.92, 0.84, -85.5, 825.81, 308, 0, 0, 0}
                , { 109.5, -116.77, -31, 125.05, 154.37, -38.75, 1804.63, -92, 0, 0, 0}
                , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
                , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
                , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
        };
        
        double sum = 0;
        for( int row = 0; row < getDataRows(); row++ )
        {
            sum = 0;
            // Do not count SUM column
            for( int column = 0; column < getDataColumns() - 1; column++ )
            {
                sum += m_arCellValues[row][column];
            }
            m_arCellValues[row][getDataColumns() - 1] = sum;
        }
    }
    
    public void recalculate_BAK()
    {
        m_arCellValues = new double[m_arRowLabels.length][m_arHeaders.length];
        ProfitLossesHolder plHolder = ValuesStorage.getInstance().getProfitLosses();
        double[] totals = new double[12];
        
        
        for( int column = 0; column < m_arHeaders.length; column++ )
        {
            for( int month = 1; month < 13; month++ )
            {
                double v = plHolder.getFutures(m_arHeaders[column], month);
                totals[month - 1] += v; 
//                GWT.log("Value of " + m_arHeaders[column] + " for month " + month + " is: " + v);
                setAt(month, column + 1, v);
            }
        }
        
        // Calculate if data is loaded
        if( ValuesStorage.getInstance().isProfitLossesFuturesLoaded() )
        {
            GWT.log("Size of cell table is: " + m_arRowLabels.length + "x" + m_arHeaders.length);
            // Display sum
            for( int i = 0; i < totals.length; i++ )
            {
                setAt(i + 1, m_arHeaders.length, totals[i]);
            }
        }
    }

    @Override
    protected void setHeaders()
    {
//        String[] arHeaders = ValuesStorage.getInstance().getProfitLosses().getExchangeNamesFutures();
//        int total = arHeaders == null ? 0 : arHeaders.length;
//        // All exchanges name + sum
//        m_arHeaders = new String[total + 1];
//        for( int i = 0; i < total; i++ )
//        {
//            m_arHeaders[i] = arHeaders[i];
//        }
//        m_arHeaders[total] = "Sum";
        m_arHeaders = new String[] { "FDAX-120", "FDAX-38", "FESX", "S&P-65", "S&P-30", "NQ", "DJ", "FGBL", "EURUSD", "YPN(Future)", "Sum" };
    }

}
